﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using System.Data;
using TaoBaoProject.Common;

namespace TaoBaoProject.Pages
{
  public partial class ShopManage : System.Web.UI.Page
  {
    protected static readonly log4net.ILog log =
   log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    ShopBLL shopBll = new ShopBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
      //
      btnDelShop.Attributes.Add("onclick", "javascript:if (document.getElementById('ListAllShops').selectedIndex == '-1') {confirm('请选择一条数据，在点 删除 按钮！谢谢。');return false;}if(confirm('确定要删除吗?如果你删除此店铺，那么在[登记]界面此店铺相关的数据将无法显示，请谨慎操作！')){}else{return false;}");
      if (!IsPostBack)
      {
        BindListData();
      }

    }
    private void BindListData()
    {
      ListAllShops.Items.Clear();
      List<Shops> allList = ConvertHelper<Shops>.ConvertToList(shopBll.GetAllShops());
      ListAllShops.DataSource = allList;
      ListAllShops.DataBind();

      for (int i = 0; i < allList.Count; i++)
      {
        ListAllShops.Items[i].Attributes.Add("style", "color:" + allList[i].ShopColor);
      }
    }



    protected void btnAddShopColor_Click(object sender, EventArgs e)
    {
      //if (txtShopName.Text.Trim().Length == 0)
      //{
      //  ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "<script>alert('店铺名不能为空！')</script>", false);
      //}
      //else
      //{
      //  Shops shop = new Shops();
      //  shop.ShopName = txtShopName.Text;
      //  shop.ShopColor = cp.Color;
      //  string errMsg = "店铺名[" + shop.ShopName + "]已存在，不能进行添加操作";
      //  try
      //  {
      //    if (!shopBll.isExistShopName(shop.ShopName))
      //    {
      //      shopBll.AddShop(shop);
      //      txtShopName.Text = "";
      //      //ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "<script>alert('添加店铺成功！')</script>", false);
      //      log.Info("添加店铺名[" + shop.ShopName + "]成功");
      //    }
      //    else
      //    {
      //      log.Info(errMsg);
      //      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "<script>alert('" + errMsg + "')</script>", false);
      //    }
      //    BindListData();
      //  }
      //  catch (Exception ex)
      //  {
      //    log.Error("在输入用户：" + shop.ShopName + "的时候，发生了异常，异常信息是： " + ex.Message);
      //  }
      //}
    }

    protected void btnDelShop_Click(object sender, EventArgs e)
    {

      //string shopID = ListAllShops.SelectedValue;
      ////string errorMsg = "确认要删除店铺[" + ListAllShops.SelectedItem + "]吗？ /r/n 如果你删除此店铺，那么在‘登记’界面此店铺相关的数据都不会显示出来，敬请谨慎";
      ////this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "message", "<script language='javascript' defer>alert('" + errorMsg + "');</script>");

      //if (shopID != null)
      //{
      //  try
      //  {
      //    shopBll.DeleteShopByID(Convert.ToInt32(shopID));
      //    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "<script>alert('删除店铺成功！')</script>", false);
      //    BindListData();
      //  }
      //  catch (Exception ex)
      //  {
      //    log.Info("在删除店铺[" + ListAllShops.SelectedItem + "]的时候发生了异常，异常信息是：" + ex.Message);
      //  }

      //}
    }

    protected void btnUpdateShopColor_Click(object sender, EventArgs e)
    {
      //Shops shop = new Shops();
      //shop.ShopName = txtShopName.Text;
      //shop.ShopColor = cp.Color;
      //string strMsg = "你输入的[" + shop.ShopName + "]店铺名不存在.";

      //try
      //{
      //  if (!shopBll.isExistShopName(shop.ShopName))
      //  {
      //    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "<script>alert('" + strMsg + "')</script>", false);
      //    return;
      //  }
      //  shopBll.UpdateShop(shop);
      //  BindListData();
      //  log.Info("成功更新了店铺[" + shop.ShopName + "]的颜色");
      //}
      //catch (Exception ex)
      //{
      //  log.Error("更新店铺名[" + shop.ShopName + "]的时候发生了异常，异常信息是：" + ex.Message);
      //}
    }
  }
}